/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package introse;

import com.sun.net.ssl.internal.ssl.Debug;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class sourceManager {
    
    private List<source> sourceList;
    
    public sourceManager() {
        loadSources();
    }
    
    public final Boolean loadSources() {
        try {
            databaseConn conn = new databaseConn();
            ResultSet sources = conn.sqlRead("SELECT * FROM source");

            List<source> tempSourceList = new ArrayList();
            while (sources.next()) {
                tempSourceList.add(new source(sources.getInt("sourceNumber"), sources.getString("sourceName"), sources.getBoolean("isActive")));
            }
            this.sourceList = tempSourceList;
            return true;
        } catch (SQLException ex) {
        }
        return false;
    }
    
    public List<source> getSources(Boolean isActive) {
        List<source> tempSources = new ArrayList();
        for (source src : sourceList) {
            if (src.getIsActive() == isActive) {
                tempSources.add(src);
            }
        }
        return tempSources;
    }
    
    public source getSource(int sourceNumber) {
        for (source src : sourceList) {
            if (src.getSourceNumber() == sourceNumber) {
                return src;
            }
        }
        return null;
    }
    
    public Boolean addSource(String name) {
        databaseConn conn = new databaseConn();
        if(conn.sqlStatement(String.format("INSERT INTO `sashmirgasul`.`source` (`sourceName`, `isActive`) VALUES ('%s', TRUE);", name))){
            loadSources();
            return true;
        }
        return false;
    }
    
}
